package org.apt.matrimony.servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apt.hanhhanh.matrimony.methods.Inserts;

/**
 *
 * @author HANHXINH
 */
public class doCompleteAccount extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String username = request.getParameter("username");
        String fullname = request.getParameter("txtName");

        String date_birt = request.getParameter("dobDay");
        String month_birt = request.getParameter("dobMonth");
        String year_birt = request.getParameter("dobYear");
        String dateofbirth = month_birt + "/" + date_birt + "/" + year_birt;

        String marital_Status = request.getParameter("MARITAL_STATUS");
        String mother_Tongue = request.getParameter("Language");
        String religion = request.getParameter("religion");
        String profess = request.getParameter("professsion");
        String height1 = request.getParameter("txtHeight");
        int height = 0;

        if (!"".equalsIgnoreCase(height1)) {
            height = Integer.parseInt(height1);
        }
        //contact Number
        String first_number = request.getParameter("txtCC");
        String lst_number = request.getParameter("txtMobile");
        String contact_Number = first_number + lst_number;

        int casteID = Integer.parseInt(request.getParameter("caste"));

        int city = Integer.parseInt(request.getParameter("city"));
        String address = request.getParameter("txtAddress");

        //insertDetailAccount(String username, String Full_Name, String Gender, String Date_Of_Birth, String Marital_Status, String Mother_Tongue, double Height, int Contact_Number,int cateID, String Address, int cityID) {
        //Inserts.insertDetailAccount(username, fullname, gender, dateofbirth, marital_Status, mother_Tongue, height, contact_Number, cateID, address, city);
        int completeAccount = 0;
        int insertReligion = 0;
        int insertProfession = 0;
        try {
            completeAccount = Inserts.insertDetailAccount(username, fullname, dateofbirth, marital_Status, mother_Tongue, height, contact_Number, casteID, address, city);
            if (completeAccount > 0) {
                insertReligion = Inserts.insertReligionDetail(religion, username);
                insertProfession = Inserts.insertProfessionDetail(profess, username);
                if (insertReligion > 0 && insertProfession > 0) {
                    request.getRequestDispatcher("completprofile2.jsp?username=" + username).forward(request, response);
                } else {
                    out.println("Religion Error!!");
                     out.println("   Religion    " +religion);
                      out.println("   Profession   "+profess);
                    out.println("     Username : "+username);
                }
            } else {
                out.println(username + "\n");
                out.println("Fulname: " + fullname + "    -     ");
                out.println(height + "\n");// Loi Cast Date
                out.println(contact_Number + "\n");
                out.println(casteID + "\n");
                out.println("  City:  " + city + "--------");
                out.println(date_birt + "\n");
                out.print("Loi Insert!");
            }

        } catch (NumberFormatException ex) {
            ex.getMessage();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
